﻿//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EffiProzDB.Lib;

namespace EffiProzDBTest.impl
{
    /// <summary>
    /// Summary description for HashMapppedListTest
    /// </summary>
    [TestClass]
    public class HashMapppedListTest
    {
        public HashMapppedListTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void EnumerationTest()
        {
           HashMappedList<int, int> listii= new HashMappedList<int,int>();

           List<int> ivals = new List<int>(50);
           List<int> svals = new List<int>(50);

            Random rand = new Random();
           for (int i = 0; i < 50;i++ )
           {
               ivals.Add(rand.Next());
               svals.Add(rand.Next());
           }

           for (int i = 0; i < 50; i++)
           {
               listii.Add(ivals[i], svals[i]);
           }

           Assert.AreEqual(ivals.Count, listii.Count);

           int count = 0;
           foreach (var pair in listii)
           {
               Assert.AreEqual(pair.Key, ivals[count]);
               Assert.AreEqual(pair.Value, svals[count]);
               count++;
           }

           count = 0;
           foreach (var ival in listii.Values)
           {
               Assert.AreEqual(ival, svals[count]);              
               count++;
           }

           count = 0;
           foreach (var ikey in listii.Keys)
           {
               Assert.AreEqual(ikey, ivals[count]);
               count++;
           }

        
           for (int i = 0; i < listii.Count; i++)
           {
                    Assert.AreEqual(listii[ivals[i]], svals[i]);
           }


           for (int i = 0; i < listii.Count; i++)
           {
               Assert.AreEqual(listii.Values[i], svals[i]);
           }


           for (int i = 0; i < listii.Count; i++)
           {
               Assert.AreEqual(listii.Keys[i], ivals[i]);
           }

           listii.Remove(ivals[25]);
           Assert.AreEqual(listii.Count, 49);
           ivals.Remove(ivals[25]);
           svals.Remove(svals[25]);
           listii.Remove(ivals[37]);
           Assert.AreEqual(listii.Count, 48);
           ivals.Remove(ivals[37]);
           svals.Remove(svals[37]);

           count = 0;
           foreach (var pair in listii)
           {
               Assert.AreEqual(pair.Key, ivals[count]);
               Assert.AreEqual(pair.Value, svals[count]);
               count++;
           }

           listii[777777987] = 897;
           Assert.AreEqual(listii.Count, 49);
           ivals.Add(777777987);
           svals.Add(897);

           count = 0;
           foreach (var pair in listii)
           {
               Assert.AreEqual(pair.Key, ivals[count]);
               Assert.AreEqual(pair.Value, svals[count]);
               count++;
           }

           listii.Values[20] = 5555555;
           svals[20] = 5555555;

           count = 0;
           foreach (var pair in listii)
           {
               Assert.AreEqual(pair.Key, ivals[count]);
               Assert.AreEqual(pair.Value, svals[count]);
               count++;
           }

           listii.Keys[31] = 78;
           ivals[31] = 78;

           count = 0;
           foreach (var pair in listii)
           {
               Assert.AreEqual(pair.Key, ivals[count]);
               Assert.AreEqual(pair.Value, svals[count]);
               count++;
           }

        }
    }
}
